#ifndef sheet_impl_h
#define sheet_impl_h

#include "../include/sheet.h"

#include<string>
#include<map>
#include<vector>



class entry_iterator_impl : public excel::entry_iterator
{
	typedef std::map<int, excel::entry*>::iterator e_iter;
public:
	entry_iterator_impl(e_iter cur, e_iter end);

	entry_iterator_impl(e_iter end);

	inline virtual cmn::dword release()
	{
		delete this;
		return 0;
	}

	virtual bool next();

	virtual bool is_equal(const excel::entry_iterator* rhs);

	virtual excel::entry* get_entry()			{ return (m_iter_cur->second); }

private:
	e_iter m_iter_cur;
	e_iter m_iter_end;
};

class sheet_impl : public excel::sheet
{
public:
	sheet_impl(cmn::kcstr memdata, int size);

	~sheet_impl();
	
	virtual excel::entry*	get_entry(int id);

	virtual int		get_type(int column);

	virtual kcwstr	get_title(int column);

	virtual int		get_column(kcwstr title);

	inline virtual int	get_column_count()		{ return m_titles.size(); }

	virtual int		get_id_column();

	virtual excel::entry_iterator*	first()		{ return new entry_iterator_impl(m_entrys.begin(), m_entrys.end()); }

	virtual excel::entry_iterator*	last()		{ return new entry_iterator_impl(m_entrys.end()); }

	inline bool is_loaded()						{ return m_is_loaded; }

	inline virtual cmn::dword release()
	{
		delete this;
		return 0;
	}

private:
	bool parse_sheet_data(kcstr memdata, int size);

	bool parse_sheet_title(kcwstr memdata, int size);

	bool parse_sheet_entry(kcwstr memdata, int size, int line);

	struct title{
		std::wstring name;
		int type;
	};

	bool	m_has_id;
	bool	m_is_loaded;
	int		m_id_col;

	std::vector<title*> m_titles;	
	std::map<int, excel::entry*> m_entrys;
};

excel::sheet* create_sheet_from_mem(cmn::kcstr memdata, int size);

#endif	//sheet_impl_h
